let webpack = require("webpack")
let Ext = require("extract-text-webpack-plugin")
let Hwp = require("html-webpack-plugin")
const path = require("path");

module.exports = {
    // entry: ["./src/main.js"],
    entry: {
        bundle :"./src/main.js",               //已多次提及的唯一入口文件
        vendor: ['react','react-dom','react-router-dom','antd','antd-mobile','axios','react-redux','react-zmage','redux','redux-promise','redux-thunk'] //在此处配置
    },
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "js/[name].[hash:8].js",
        publicPath: ""
    },
    // devtool: "source-map",
    devtool: false,
    // devServer: {
    //     contentBase: path.join(__dirname, "dist"),
    //     port: 7001,
    //     inline: true,
    //     historyApiFallback: true,
    //     disableHostCheck: true,
    //     proxy: {
    //         "/api": {
    //             "target": "http://baobab.kaiyanapp.com/",
    //             "changeOrigin": true,
    //             "secure": false,
    //         },
    //         "/ecs": {
    //             target: "http://59.110.237.148:6602/",
    //             "changeOrigin": true,
    //             "secure": false,
    //         }
    //     }
    // },
    module: {
        rules: [
            {
                test: /\.css$/,
                loader: Ext.extract("css-loader")
            },
            {
                test: /\.(css|scss)$/,
                use: Ext.extract({
                    fallback: "style-loader",  // 转为 Node 风格代码 
                    use: [
                        'css-loader',  // 变成JS 模块
                        {
                            loader: "postcss-loader",   // 编译CSS
                            options: {
                                plugins: function () {
                                    return [
                                        // require("cssgrace"),    // 美化CSS 代码 bug white-space 强制使用
                                        require('postcss-px2rem-exclude')(
                                            {
                                                remUnit: 100,
                                                exclude: /antd-mobile/i,  // 排除 antd-mobile不需要进行 rem 转换 
                                            }
                                        ), // px 转 rem 
                                        require("autoprefixer")  // 自动补全 
                                    ]
                                }
                            }
                        },
                        "sass-loader"
                    ]
                })
            },
            {
                test: /\.(css|less)/,
                use: Ext.extract({
                    fallback: "style-loader",  // 转为 Node 风格代码 
                    use: [
                        'css-loader',  // 变成JS 模块
                        {
                            loader: "postcss-loader",   // 编译CSS
                            options: {
                                plugins: function () {
                                    return [
                                        // require("cssgrace"),    // 美化CSS 代码 
                                        require('postcss-px2rem-exclude')(
                                            {
                                                remUnit: 100,
                                                exclude: /antd-mobile/i,  // 排除 antd-mobile不需要进行 rem 转换 
                                            }
                                        ), // px 转 rem 
                                        require("autoprefixer")  // 自动补全 
                                    ]
                                }
                            }
                        },
                        "less-loader"
                    ]
                })
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            },
            {
                test: /\.(png|gif|svg|jpg|jpeg|woff|woff2|eot|ttf|ico)\??.*$/,
                use: ["file-loader?limit=8192&name=font/[name].[hash:8].[ext]"]
            },
        ]
    },
    plugins: [
        new webpack.ProvidePlugin({
            React: "react",
            Axios: "axios"
        }),
        new Ext({
            filename: "css/app.[hash:8].css",
            allChunks: true,
            disable: false
        }),
        new Hwp({
            template: "index.html",
            filename: "index.html",
            inject: true,
            favicon: path.resolve('favicon.ico')
        }),
        new webpack.optimize.CommonsChunkPlugin({ 
            name: 'vendor', 
            filename: 'vendor.bundle.js' 
        }),
    ]
}